angular.module("app.login")
    .controller("loginCtrl", ["$rootScope", "$scope", "toaster", "loginService",
        function($rootScope, $scope, toaster, loginService) {
        if(loginService.isLoggedIn()) {
            window.location.href = "index.html" + "?" + Date.now();
            return;
        }

        $scope.login = function() {
            if(!$scope.user || !$scope.user.userName) {
               toaster.pop("warning","提示", "请输入用户名");
               return;
            }

            if(!$scope.user.password) {
                toaster.pop("warning","提示", "请输入密码");
                return;
            }

            $rootScope.loading = true;
            loginService.login($scope.user, $scope.rememberMe).then(function() {
                $rootScope.loading = false;
                window.location.href = "index.html"  + "?" + Date.now();
            }, function(err) {
                $rootScope.loading = false;
                toaster.pop("error","错误", err);
            });
        };
    }]);
